package cn.imu.entity;

import cn.imu.Utils.CryptoUtil;
import cn.imu.Utils.RSACoder;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.security.NoSuchAlgorithmException;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Wallet {
    private String publicKey;//公钥
    private String privateKey;//私钥

    /*
    只含有公钥的钱包，用来给其他节点使用，其他节点在转账时需要用到
     */
    public Wallet(String publicKey){
        this.publicKey=publicKey;
    }

    public static Wallet generateWallet(){
        Map<String,Object> initKey;
        try{
            //本地生成公私钥对
            initKey= RSACoder.initKey();
            String publicKey=RSACoder.getPublicKey(initKey);
            String privateKey=RSACoder.getPrivateKey(initKey);
            return new Wallet(publicKey,privateKey);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
    /*
    获取钱包地址,使用公钥生成
     */
    public String getAddress(){
        String publicKeyHash=hashPubKey(publicKey);
        return CryptoUtil.MD5(publicKeyHash);
    }

    /*
    获取钱包公钥hash
     */
    public String getHashPubKey(){
        return CryptoUtil.SHA256(publicKey);
    }

    /*
    生成钱包公钥hash
     */
     public static String hashPubKey(String publicKey){
         return CryptoUtil.SHA256(publicKey);
     }
}
